//package com.holly.unit.auth.config;
//
//import cn.hutool.core.date.DateUnit;
//import cn.hutool.core.date.DateUtil;
//import com.holly.unit.auth.api.LoginUserApi;
//import com.holly.unit.auth.api.context.LoginContext;
//import com.holly.unit.auth.api.exception.AuthException;
//import com.holly.unit.auth.api.exception.enums.AuthExceptionEnum;
//import com.holly.unit.auth.api.pojo.login.LoginUser;
//import com.holly.unit.system.api.HisUserPasswordServiceApi;
//import com.holly.unit.system.api.NoticeServiceApi;
//import com.holly.unit.system.api.constants.SystemConstants;
//import com.holly.unit.system.api.pojo.notice.SysNoticeRequest;
//import com.holly.unit.system.api.pojo.user.HisUserPasswordDTO;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.annotation.Resource;
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Date;
//
///**
// * 密码过期策略
// */
//@Slf4j
//@Component
//@Order(100)
//public class AuthPasswordPolicyFilter extends OncePerRequestFilter {
//
//    @Resource
//    private HisUserPasswordServiceApi hisUserPasswordServiceApi;
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
//            throws ServletException, IOException {
//        //如果用户已经登录，则校验密码是否过期，及用户是否第一次登录
//        LoginUserApi loginUserApi = LoginContext.me();
//        if (loginUserApi != null) {
//            if (loginUserApi.hasLogin()) {
//                //密码过期不校验白名单
//                AntPathMatcher antPathMatcher = new AntPathMatcher();
//                String[] excludeFirstUpdates = {"/**/sysUser/resetPwd", "/**/logoutAction","/**/loginApi", "/**/sysUser/updatePassword","/**/sys/user/updatePassword","/**/dragCaptcha"};
//                for (String excludeFirstUpdate : excludeFirstUpdates) {
//                    if (antPathMatcher.match(excludeFirstUpdate, request.getRequestURI())) {
//                        filterChain.doFilter(request, response);
//                        return;
//                    }
//                }
//                //判断用户密码是否过期，默认是90天内
//                this.passwordExpireRemind(request.getRequestURI());
//            }
//        }
//        filterChain.doFilter(request, response);
//    }
//
//    public void passwordExpireRemind(String requestUrl) throws RuntimeException {
//        LoginUser user = LoginContext.me().getLoginUser();
//        // 密码有效期（天数）
//        HisUserPasswordDTO userLastHisPassword = hisUserPasswordServiceApi.findPasswordExpiringUser(user.getUserId());
//
//        if (userLastHisPassword == null) {
//            throw new AuthException(AuthExceptionEnum.USERNAME_TIMEOUT_ERROR);
//        }else{
//            if(userLastHisPassword.getType().equals(SystemConstants.HP_TYPE_ADD))
//                throw new AuthException(AuthExceptionEnum.SSO_LOGIN_FIRST);
//        }
//    }
//}
